Auto complete
Allow users to speed up data filtering by receiving live search suggestions as they type
#Examples
The AutoComplete component is a single-line text input field that dynamically suggests options from a pre-defined list based on user input. It is designed to improve the user experience by reducing the number of keystrokes required to complete a task and increasing the accuracy of input.
#Variant overview:
| Variant | Purpose | Usage guideline |
|---|---|---|
| Default | Selecting from a known, predefined list of options. | Suggestions update in real-time as the user types. |
| Async | Filtering through large or dynamic datasets sourced from a database or API. | Fetches suggestions on-the-fly; includes loading indicator during retrieval. |
| Invalid | Informing the user of incorrect input that doesn't match available options or violates validation rules. | Inherits styles from the Input field component; displays clear error messages. |
| Disabled | When the Autocomplete is temporarily unavailable or irrelevant in the current context. | Prevents user interaction; can include a hint text or a visual clue explaining the disabled state. |
#Default
The default variant is the primary mode for user interaction. It functions by dynamically updating the list of suggestions in real time as the user types, making it ideal for scenarios where users need to select from a predefined set of options. To ensure a clear and uncluttered user experience, it's recommended to limit the number of suggestions displayed simultaneously.
Composition:
- Input field: The primary area where users type their query.
- Suggestion list: A dynamic list of options appears below the input field, updating as the user types, with matched text highlighted.
- Loading indicator: A visual cue (e.g., spinner) indicating suggestions are being fetched from an external source.
- Error message: Clear feedback displayed when the input is invalid or doesn't match available options.
const [value, setValue] = useState("");
return (
<AutoComplete
suggestions={[
"Apple",
"Banana",
"Blueberry",
"Cherry",
"Grape",
"Guava",
"Lemon",
"Lime",
"Orange",
"Peach",
"Pear",
"Pineapple",
"Raspberry",
"Strawberry",
"Watermelon",
]}
value={value}
onChange={setValue}
name="fruit"
placeholder="Type a fruit"
aria-label="List of fruits"
/>
);#Async
The Async variant is designed to handle situations where the list of suggestions is extensive or needs to be dynamically updated. It retrieves options from an database or API, or hard-coded into the component, eliminating the need to load all possibilities upfront. During the retrieval process, a Spinner is displayed to provide visual feedback to the user.
const [suggestions, setSuggestions] = useState<string[]>([]);
const [value, setValue] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const debouncedSearch = useDebounceFn(onSearch, 500);
async function onSearch(searchQuery: string, caseSensitive: boolean) {
const data = await api.searchFruits(searchQuery, caseSensitive);
setSuggestions(data.map((fruit) => fruit.name));
setLoading(false);
}
return (
<AutoComplete
loading={loading}
suggestions={suggestions}
value={value}
onChange={setValue}
onSearch={(q) => {
setLoading(true);
debouncedSearch(q, false);
}}
name="fruit"
placeholder="Type a fruit"
aria-label="List of fruits"
/>
);#Invalid
The Invalid variant, inheriting all states and visual properties, like disabled, invalid, and also visual properties like fullWidth, custom width, large size, leftSlug and rightSlug, etc. from the Input field component, is designed to indicate erroneous input.
This can include situations where the entered value doesn't match any suggestions, violates validation rules, or is simply left empty. Clear error messages are essential in these scenarios, providing users with information about the issue and guidance on resolving it.
Ideally, data validation should occur before form submission to minimize user frustration. While error messages can highlight empty fields, ensuring proper labeling of required fields beforehand enhances user understanding and reduces the likelihood of error (see Best Practices in Input field). Follow the writing guideline for Issues and issue descriptions.
Some error message to let the user know what's wrong
const [value, setValue] = useState("");
return (
<FormElementWrapper
label="Fruit"
name="Fruit"
invalid
error="Some error message to let the user know what's wrong"
>
<AutoComplete
suggestions={["Apple", "Banana", "Blueberry", "Cherry"]}
value={value}
onChange={setValue}
placeholder="Type a fruit"
/>
</FormElementWrapper>
);#Disabled
The Disabled variant serves to visually indicate that the component is either temporarily unavailable or not applicable in the current situation. By maintaining the AutoComplete's presence in the layout but preventing user interaction, it ensures a consistent visual experience.
Whenever possible, a hint text or a visual clue (e.g Tooltip) should be included to clarify the reason for the component's disabled state, enhancing the overall user understanding.
const [value, setValue] = useState("");
return (
<AutoComplete
disabled
suggestions={["Item 1", "Item 2", "Item 3"]}
value={value}
onChange={setValue}
name="some input"
placeholder="Placeholder"
aria-label="List of suggestions"
/>
);#Properties
| Property | Description | Defined | Value |
|---|---|---|---|
valueRequired | stringValue of the form control | ||
onChangeRequired | functionCallback for onChange event | ||
suggestionsRequired | string[]List of options to display in the listbox | ||
nameOptional | stringName applied to the form control | ||
idOptional | stringId applied to the form control | ||
invalidOptional | booleanIs the form control invalid | ||
onBlurOptional | functionCallback for onBlur event | ||
aria-labelOptional | stringLabel of the form control | ||
aria-describedbyOptional | stringID of an an element that describes what the form control is for | ||
aria-labelledbyOptional | stringID of an an element that labels this form control | ||
onSearchOptional | functionHandler for searching event | ||
caseSensitiveOptional | booleanEnables case sensitive highlight | ||
loadingOptional | booleanDisplay a loading spinner | ||
placementOptional | "auto" | "auto-end" | "auto-start" | "bottom" | "bottom-end" | "bottom-start" | "left" | "left-end" | "left-start" | "right" | "right-end" | "right-start" | "top" | "top-end" | "top-start"Preferred placement for the popover | ||
allowedAutoPlacementsOptional | literal-union[]Allowed placements for the popover when using an "auto" value for the "placement" prop | ||
disabledOptional | boolean | ||
largeOptional | boolean | ||
widthOptional | number | ||
fullWidthOptional | boolean | ||
autoFocusOptional | boolean | ||
placeholderOptional | string | ||
leftSlugOptional | element | ||
rightSlugOptional | element | ||
data-observe-keyOptional | stringUnique string, used by external script e.g. for event tracking | ||
classNameOptional | stringCustom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | objectStyle object to apply custom inline styles (only intended for special cases) | ||
tabIndexOptional | numberTab index of the outermost HTML element of the component | ||
onKeyDownOptional | functionCallback for onKeyDown event | ||
onMouseDownOptional | functionCallback for onMouseDown event | ||
onMouseEnterOptional | functionCallback for onMouseEnter event | ||
onMouseLeaveOptional | functionCallback for onMouseLeave event | ||
onFocusOptional | functionCallback for onFocus event |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications